package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.bean.Ordergoods;
import com.lovo.bean.Tenderinfo2;
import com.lovo.dto.OrderGoodsDto;
import com.lovo.dto.TenderInfoDto;
import com.lovo.mapper.OrdergoodsMapper;
import com.lovo.service.IOrdergoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * ??????Ʒ? 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
public class OrdergoodsServiceImpl extends ServiceImpl<OrdergoodsMapper, Ordergoods> implements IOrdergoodsService {


    @Autowired
    private OrdergoodsMapper ordergoodsMapper;

    @Override
    public List<Ordergoods> findAllOrderGoods(Integer orderId) {
        QueryWrapper<Ordergoods> qw = new QueryWrapper<>();
        qw.eq("orderId", orderId);
        List<Ordergoods> list = ordergoodsMapper.selectList(qw);
        return list;
    }

    @RabbitListener(queues = "sellGoodsQueue")
    public void addOrderGoods(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, List<OrderGoodsDto> orderGoodsDtoList) throws IOException {

        orderGoodsDtoList.forEach(n -> {
            Ordergoods ordergoods = new Ordergoods();
            BeanUtils.copyProperties(n, ordergoods);
            ordergoodsMapper.insert(ordergoods);
        });

        //手动确认
        channel.basicAck(tag, false);
    }


}
